package com.yanqu.road.server.cmd.decoration;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.MammonConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.mammon.MammonMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;


@Cmd(code = Protocol.S_MAMMON_RECORD_LIST, desc = "财神簿获取")
public class MammonRecordListCmd extends AbstractCommand {

	private int decorationId;
	private DecorationProto.MammonRecordListMsg.Builder recordListMsg;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		DecorationProto.MammonRecordListReqMsg reqMsg = DecorationProto.MammonRecordListReqMsg.parseFrom(packet.getBytes());
		decorationId = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!MammonConfigMgr.isMammonDecorationId(decorationId)){
			ret = GameErrorCode.E_DECORATION_NO_MAMMON;
			return;
		}
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Mammon.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if (MammonConfigMgr.isCrossMammonDecorationId(decorationId)
				|| MammonConfigMgr.isCrossAreaMammonDecorationId(decorationId)) {
			recordListMsg = CrossDataMgr.getCrossGroupMammonRecordMsg(decorationId);
		}else {
			recordListMsg = MammonMgr.getMammonRecordListMsg(decorationId);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		DecorationProto.MammonRecordListMsg.Builder respMsg = recordListMsg;
		if(null == respMsg){
			respMsg = DecorationProto.MammonRecordListMsg.newBuilder();
		}
		player.sendPacket(Protocol.U_MAMMON_RECORD_LIST, respMsg);
	}

}
